﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Betop.Lib.Core.Base;
using Betop.Lib.Web;

namespace Betop.Lib.Core.Dict
{
    public class DictDAL : BaseDAL
    {
        public List<DictInfo> GetList(int item, PagerSortSetting pagerSetting)
        {
            string sql = "select * from Dict where UpperID = " + item + " order by Orders";

            List<DictInfo> list = dbOper.Query<DictInfo>(sql);

            if (pagerSetting != null)
            {
                pagerSetting.RecordCount = list.Count();
                list = list.Skip(pagerSetting.SkipCount).Take(pagerSetting.PageSize).ToList();
            }
            return list;
        }

        public DataTable GetDataTable(int item)
        {
            string sql = "select * from Dict where UpperID = " + item + " order by Orders";

            DataTable dt = dbOper.Query(sql);

            return dt;
        }

        public List<DictInfo> GetList(int item)
        {
            string sql = "select * from Dict where UpperID = " + item + " order by Orders";
            List<DictInfo> dicts = dbOper.Query<DictInfo>(sql);
            return dicts;
        }

        internal bool ExistCode(int code, string strID, int upperID)
        {
            string sql = "select count(*) from Dict where UpperID = @p0 and Code = @p1";
            int count;
            if (strID != null && strID != "")
            {
                sql += " and ID != @p2";
                count = dbOper.GetScalar<int>(sql, upperID, code, strID);
            }
            else
            {
                count = dbOper.GetScalar<int>(sql, upperID, code);
            }
            bool flag = (count == 0) ? false : true;
            return flag;
        }

        internal int GetMaxCode(int item)
        {
            string sql = "select isnull(max(Code),0) + 1 from Dict where UpperID = " + item;
            int code = dbOper.GetScalar<int>(sql);
            return code;
        }


        internal string GetName(int dictItem, int code)
        {
            string sql = "select Name from Dict where UpperID = " + dictItem + " and Code = " + code;
            DataTable dt = dbOper.Query(sql);
            if (dt.Rows.Count == 0)
            {
                return "";
            }
            else
            {
                return dt.Rows[0]["Name"].ToString();
            }
        }
    }
}
